﻿using DbContext;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext;
using SmartPlant.Modules.Base.Views;
using SmartPlant.Services;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls.Map;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx;
using Telerik.Windows.Documents.Spreadsheet.Model;
using Telerik.Windows.Persistence.Core;
using Unity;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using Line = SmartPlant.Model.DbContext.Line;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class PdGeneralEditViewModel : RegionViewModelBase
    {


        private ObservableCollection<DropDownListObj> _list_pdFluidPhase;

        public ObservableCollection<DropDownListObj> list_pdFluidPhase
        {
            get { return _list_pdFluidPhase; }
            set
            {
                SetProperty(ref _list_pdFluidPhase, value);
            }
        }


        private ObservableCollection<DropDownListObj> _list_pdFlowUid;

        public ObservableCollection<DropDownListObj> list_pdFlowUid
        {
            get { return _list_pdFlowUid; }
            set
            {
                SetProperty(ref _list_pdFlowUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdDensUid;

        public ObservableCollection<DropDownListObj> list_PdDensUid
        {
            get { return _list_PdDensUid; }
            set
            {
                SetProperty(ref _list_PdDensUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressUid;
        public ObservableCollection<DropDownListObj> list_PdPressUid
        {
            get { return _list_PdPressUid; }
            set
            {
                SetProperty(ref _list_PdPressUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdTempUid;
        public ObservableCollection<DropDownListObj> list_PdTempUid
        {
            get { return _list_PdTempUid; }
            set
            {
                SetProperty(ref _list_PdTempUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdViscUid;
        public ObservableCollection<DropDownListObj> list_PdViscUid
        {
            get { return _list_PdViscUid; }
            set
            {
                SetProperty(ref _list_PdViscUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdVelocityUid;
        public ObservableCollection<DropDownListObj> list_PdVelocityUid
        {
            get { return _list_PdVelocityUid; }
            set
            {
                SetProperty(ref _list_PdVelocityUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdVapPressUid;
        public ObservableCollection<DropDownListObj> list_PdVapPressUid
        {
            get { return _list_PdVapPressUid; }
            set
            {
                SetProperty(ref _list_PdVapPressUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdCriticPressUid;
        public ObservableCollection<DropDownListObj> list_PdCriticPressUid
        {
            get { return _list_PdCriticPressUid; }
            set
            {
                SetProperty(ref _list_PdCriticPressUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDesUid;
        public ObservableCollection<DropDownListObj> list_PdPressDesUid
        {
            get { return _list_PdPressDesUid; }
            set
            {
                SetProperty(ref _list_PdPressDesUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDesMinUid;
        public ObservableCollection<DropDownListObj> list_PdPressDesMinUid
        {
            get { return _list_PdPressDesMinUid; }
            set
            {
                SetProperty(ref _list_PdPressDesMinUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdDesignTempUid;
        public ObservableCollection<DropDownListObj> list_PdDesignTempUid
        {
            get { return _list_PdDesignTempUid; }
            set
            {
                SetProperty(ref _list_PdDesignTempUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdVapPressUflg;
        public ObservableCollection<DropDownListObj> list_PdVapPressUflg
        {
            get { return _list_PdVapPressUflg; }
            set
            {
                SetProperty(ref _list_PdVapPressUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressUflg;
        public ObservableCollection<DropDownListObj> list_PdPressUflg
        {
            get { return _list_PdPressUflg; }
            set
            {
                SetProperty(ref _list_PdPressUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDesUflg;
        public ObservableCollection<DropDownListObj> list_PdPressDesUflg
        {
            get { return _list_PdPressDesUflg; }
            set
            {
                SetProperty(ref _list_PdPressDesUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdFlowUflg;
        public ObservableCollection<DropDownListObj> list_PdFlowUflg
        {
            get { return _list_PdFlowUflg; }
            set
            {
                SetProperty(ref _list_PdFlowUflg, value);
            }
        }

        private ObservableCollection<DropDownListObj> _list_PdCriticPressUflg;
        public ObservableCollection<DropDownListObj> list_PdCriticPressUflg
        {
            get { return _list_PdCriticPressUflg; }
            set
            {
                SetProperty(ref _list_PdCriticPressUflg, value);
            }
        }

        private ObservableCollection<DropDownListObj> _list_PdPressDrpUid;
        public ObservableCollection<DropDownListObj> list_PdPressDrpUid
        {
            get { return _list_PdPressDrpUid; }
            set
            {
                SetProperty(ref _list_PdPressDrpUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDownUid;
        public ObservableCollection<DropDownListObj> list_PdPressDownUid
        {
            get { return _list_PdPressDownUid; }
            set
            {
                SetProperty(ref _list_PdPressDownUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDownUflg;
        public ObservableCollection<DropDownListObj> list_PdPressDownUflg
        {
            get { return _list_PdPressDownUflg; }
            set
            {
                SetProperty(ref _list_PdPressDownUflg, value);
            }
        }

        private ObservableCollection<DropDownListObj> _list_PdPressDesMinUflg;
        public ObservableCollection<DropDownListObj> list_PdPressDesMinUflg
        {
            get { return _list_PdPressDesMinUflg; }
            set
            {
                SetProperty(ref _list_PdPressDesMinUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdReturnTempUid;
        public ObservableCollection<DropDownListObj> list_PdReturnTempUid
        {
            get { return _list_PdReturnTempUid; }
            set
            {
                SetProperty(ref _list_PdReturnTempUid, value);
            }

        }
        private ObservableCollection<DropDownListObj> _list_PdReturnPresUid;
        public ObservableCollection<DropDownListObj> list_PdReturnPresUid
        {
            get { return _list_PdReturnPresUid; }
            set
            {
                SetProperty(ref _list_PdReturnPresUid, value);
            }

        }
        private ObservableCollection<DropDownListObj> _list_PdReturnPresUflg;
        public ObservableCollection<DropDownListObj> list_PdReturnPresUflg
        {
            get { return _list_PdReturnPresUflg; }
            set
            {
                SetProperty(ref _list_PdReturnPresUflg, value);
            }

        }
        private ObservableCollection<DropDownListObj> _list_LINE_ID;
        public ObservableCollection<DropDownListObj> list_LINE_ID
        {
            get { return _list_LINE_ID; }
            set
            {
                SetProperty(ref _list_LINE_ID, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdColored;
        public ObservableCollection<DropDownListObj> list_PdColored
        {
            get { return _list_PdColored; }
            set
            {
                SetProperty(ref _list_PdColored, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdErosive;
        public ObservableCollection<DropDownListObj> list_PdErosive
        {
            get { return _list_PdErosive; }
            set
            {
                SetProperty(ref _list_PdErosive, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdCorrosive;
        public ObservableCollection<DropDownListObj> list_PdCorrosive
        {
            get { return _list_PdCorrosive; }
            set
            {
                SetProperty(ref _list_PdCorrosive, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdToxic;
        public ObservableCollection<DropDownListObj> list_PdToxic
        {
            get { return _list_PdToxic; }
            set
            {
                SetProperty(ref _list_PdToxic, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_Case_Id;
        public ObservableCollection<DropDownListObj> list_Case_Id
        {
            get { return _list_Case_Id; }
            set
            {
                SetProperty(ref _list_Case_Id, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdFluidStat;
        public ObservableCollection<DropDownListObj> list_PdFluidStat
        {
            get { return _list_PdFluidStat; }
            set
            {
                SetProperty(ref _list_PdFluidStat, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdSource;
        public ObservableCollection<DropDownListObj> list_PdSource
        {
            get { return _list_PdSource; }
            set
            {
                SetProperty(ref _list_PdSource, value);
            }
        }


        private PdGeneral _data;

        private ICommand _phaseChange;

        public ICommand phaseChange
        {
            get { return _phaseChange; }
            set { _phaseChange = value; }
        }

        public PdGeneral data
        {
            get { return _data; }
            set
            {
                if (SetProperty(ref _data, value))
                {
                    if (_data != null)
                    {
                        _data.PropertyChanged += (sender, args) =>
                        {
                            if (args.PropertyName == nameof(data.PdFluidPhase))
                            {
                                UpdateVisibility();
                            }
                        };
                    }
                }
            }
        }
        private string _LineSize;
        public string LineSize
        {
            get { return _LineSize; }
            set { SetProperty(ref _LineSize, value); }
        }
        private string _LineUom;
        public string LineUom
        {
            get { return _LineUom; }
            set { SetProperty(ref _LineUom, value); }
        }
        private string _LineSche;
        public string LineSche
        {
            get { return _LineSche; }
            set { SetProperty(ref _LineSche, value); }
        }
        private SmartPlant.Model.DbContext.Line _Linedata2;
        public SmartPlant.Model.DbContext.Line Linedata2
        {
            get { return _Linedata2; }
            set
            {
                SetProperty(ref _Linedata2, value);
            }
        }
        private async void UpdateLinedata()
        {
            Linedata2 = await LineServ.GetById(Componentdata.LINE_ID);
            LineSize = Linedata2.LineSize;
            LineUom = Linedata2.LineUom;
            LineSche = Linedata2.LineSched;
        }
        private void UpdateVisibility()
        {
            if (data != null)
            {
                LNotVisible = data.PdFluidPhase == "L";
                PNotVisible = data.PdFluidPhase == "P";
                WNotVisible = data.PdFluidPhase == "W";
                GNotVisible = data.PdFluidPhase == "G";
                SNotVisible = data.PdFluidPhase == "S";
                LPWSNotVisible = (data.PdFluidPhase == "P" || data.PdFluidPhase == "L" || data.PdFluidPhase == "W" || data.PdFluidPhase == "S");
                GPSNotVisible = (data.PdFluidPhase == "P" || data.PdFluidPhase == "G" || data.PdFluidPhase == "S");
                LPWNotVisible = (data.PdFluidPhase == "P" || data.PdFluidPhase == "L" || data.PdFluidPhase == "W");

            }
        }
        private bool _LPWNotVisible;
        public bool LPWNotVisible
        {
            get => _LPWNotVisible;
            set => SetProperty(ref _LPWNotVisible, value);
        }
        private bool _LPWSNotVisible;
        public bool LPWSNotVisible
        {
            get => _LPWSNotVisible;
            set => SetProperty(ref _LPWSNotVisible, value);
        }
        private bool _GPSNotVisible;
        public bool GPSNotVisible
        {
            get => _GPSNotVisible;
            set => SetProperty(ref _GPSNotVisible, value);
        }

        private bool _LNotVisible;
        public bool LNotVisible
        {
            get => _LNotVisible;
            set => SetProperty(ref _LNotVisible, value);
        }
        private bool _PNotVisible;
        public bool PNotVisible
        {
            get => _PNotVisible;
            set => SetProperty(ref _PNotVisible, value);
        }
        private bool _WNotVisible;
        public bool WNotVisible
        {
            get => _WNotVisible;
            set => SetProperty(ref _WNotVisible, value);
        }
        private bool _GNotVisible;
        public bool GNotVisible
        {
            get => _GNotVisible;
            set => SetProperty(ref _GNotVisible, value);
        }
        private bool _SNotVisible;
        public bool SNotVisible
        {
            get => _SNotVisible;
            set => SetProperty(ref _SNotVisible, value);
        }
        private bool _isControlAnalyzerVisible;
        public bool IsControlAnalyzerVisible
        {
            get => _isControlAnalyzerVisible;
            set => SetProperty(ref _isControlAnalyzerVisible, value);
        }
        private bool _isControlCVisible;
        public bool IsControlCVisible
        {
            get => _isControlCVisible;
            set => SetProperty(ref _isControlCVisible, value);
        }
        private bool _IsControlGeneralVisible;
        public bool IsControlGeneralVisible
        {
            get => _IsControlGeneralVisible;
            set => SetProperty(ref _IsControlGeneralVisible, value);
        }
        private bool _isControlTempVisible;
        public bool IsControlTempVisible
        {
            get => _isControlTempVisible;
            set => SetProperty(ref _isControlTempVisible, value);
        }
        private bool _isControlFlowVisible;
        public bool IsControlFlowVisible
        {
            get => _isControlFlowVisible;
            set => SetProperty(ref _isControlFlowVisible, value);
        }
        private bool _isControlLevelVisible;
        public bool IsControlLevelVisible
        {
            get => _isControlLevelVisible;
            set => SetProperty(ref _isControlLevelVisible, value);
        }
        private bool _isControlPressVisible;
        public bool IsControlPressVisible
        {
            get => _isControlPressVisible;
            set => SetProperty(ref _isControlPressVisible, value);
        }
        private bool _isControlControlValveVisible;
        public bool IsControlControlValveVisible
        {
            get => _isControlControlValveVisible;
            set => SetProperty(ref _isControlControlValveVisible, value);
        }
        private bool _isCombinedControlVisible;
        public bool IsCombinedControlVisible
        {
            get => _isCombinedControlVisible;
            set => SetProperty(ref _isCombinedControlVisible, value);
        }
        private string _labelText;
        public string LabelText
        {
            get { return _labelText; }
            set { SetProperty(ref _labelText, value); }
        }
        private decimal? _AlarmLow;
        public decimal? AlarmLow
        {
            get { return _AlarmLow; }
            set { SetProperty(ref _AlarmLow, value); }
        }
        private decimal? _AlarmLowLow;
        public decimal? AlarmLowLow
        {
            get { return _AlarmLowLow; }
            set { SetProperty(ref _AlarmLowLow, value); }
        }
        private decimal? _AlarmLowLowLow;
        public decimal? AlarmLowLowLow
        {
            get { return _AlarmLowLowLow; }
            set { SetProperty(ref _AlarmLowLowLow, value); }
        }
        private decimal? _TripLow;
        public decimal? TripLow
        {
            get { return _TripLow; }
            set { SetProperty(ref _TripLow, value); }
        }
        private decimal? _TripLowLow;
        public decimal? TripLowLow
        {
            get { return _TripLowLow; }
            set { SetProperty(ref _TripLowLow, value); }
        }
        private decimal? _TripLowLowLow;
        public decimal? TripLowLowLow
        {
            get { return _TripLowLowLow; }
            set { SetProperty(ref _TripLowLowLow, value); }
        }
        private decimal? _TripHigh;
        public decimal? TripHigh
        {
            get { return _TripHigh; }
            set { SetProperty(ref _TripHigh, value); }
        }
        private decimal? _TripHighHigh;
        public decimal? TripHighHigh
        {
            get { return _TripHighHigh; }
            set { SetProperty(ref _TripHighHigh, value); }
        }
        private decimal? _TripHighHighHigh;
        public decimal? TripHighHighHigh
        {
            get { return _TripHighHighHigh; }
            set { SetProperty(ref _TripHighHighHigh, value); }
        }
        private decimal? _AlarmHigh;
        public decimal? AlarmHigh
        {
            get { return _AlarmHigh; }
            set { SetProperty(ref _AlarmHigh, value); }
        }
        private decimal? _AlarmHighHigh;
        public decimal? AlarmHighHigh
        {
            get { return _AlarmHighHigh; }
            set { SetProperty(ref _AlarmHighHigh, value); }
        }
        private decimal? _AlarmHighHighHigh;
        public decimal? AlarmHighHighHigh
        {
            get { return _AlarmHighHighHigh; }
            set { SetProperty(ref _AlarmHighHighHigh, value); }
        }
        private Analyzer _Aanalyzerdata;
        public Analyzer Analyzerdata
        {
            get => _Aanalyzerdata;
            set => SetProperty(ref _Aanalyzerdata, value);
        }
        private Temperature _Temperaturedata;
        public Temperature Temperaturedata
        {
            get => _Temperaturedata;
            set => SetProperty(ref _Temperaturedata, value);
        }
        private Pressure _Pressuredata;
        public Pressure Pressuredata
        {
            get => _Pressuredata;
            set => SetProperty(ref _Pressuredata, value);
        }
        private Flow _Flowdata;
        public Flow Flowdata
        {
            get => _Flowdata;
            set => SetProperty(ref _Flowdata, value);
        }

        private Instruments _Componentdata;

        public Instruments Componentdata
        {
            get { return _Componentdata; }
            set
            {
                if (SetProperty(ref _Componentdata, value))
                {
                    if (_Componentdata != null)
                    {
                        _Componentdata.PropertyChanged += (sender, args) =>
                        {
                            if (args.PropertyName == nameof(Componentdata.LINE_ID))
                            {
                                UpdateLinedata();
                            }
                        };
                };
             }
            } 
        }

        private SmartPlant.Model.DbContext.Line _Linedata;

        public SmartPlant.Model.DbContext.Line Linedata
        {
            get { return _Linedata; }
            set
            {
                SetProperty(ref _Linedata, value);
            }
        }

        private DelegateCommand<object> _AddCaseCommand;

        public ICommand AddCaseCommand
        {
            get
            {
                if (_AddCaseCommand != null) return _AddCaseCommand;
                _AddCaseCommand = new DelegateCommand<object>(onSaveCommand);
                return _AddCaseCommand;
            }
        }
        private DelegateCommand<object> _DeleteCaseCommand;

        public ICommand DeleteCaseCommand
        {
            get
            {
                if (_DeleteCaseCommand != null) return _DeleteCaseCommand;
                _DeleteCaseCommand = new DelegateCommand<object>(onSaveCommand);
                return _DeleteCaseCommand;
            }
        }
        private DelegateCommand<object> _ChangeCaseCommand;

        public ICommand ChangeCaseCommand
        {
            get
            {
                if (_ChangeCaseCommand != null) return _ChangeCaseCommand;
                _ChangeCaseCommand = new DelegateCommand<object>(onSaveCommand);
                return _ChangeCaseCommand;
            }
        }
        private DelegateCommand<object> _PrintCommand;

        public ICommand PrintCommand
        {
            get
            {
                if (_PrintCommand != null) return _PrintCommand;
                _PrintCommand = new DelegateCommand<object>(onSaveCommand);
                return _PrintCommand;
            }
        }

        private DelegateCommand<object> _CopyFromCommand;

        public ICommand CopyFromCommand
        {
            get
            {
                if (_CopyFromCommand != null) return _CopyFromCommand;
                _CopyFromCommand = new DelegateCommand<object>(onSaveCommand);
                return _CopyFromCommand;
            }
        }
        private DelegateCommand<object> _RevionManagerCommand;

        public ICommand RevionManagerCommand
        {
            get
            {
                if (_RevionManagerCommand != null) return _RevionManagerCommand;
                _RevionManagerCommand = new DelegateCommand<object>(onRevisionCommand);
                return _RevionManagerCommand;
            }
        }
        private DelegateCommand<object> _LineProcessDataCommand;

        public ICommand LineProcessDataCommand
        {
            get
            {
                if (_LineProcessDataCommand != null) return _LineProcessDataCommand;
                _LineProcessDataCommand = new DelegateCommand<object>(onSaveCommand);
                return _LineProcessDataCommand;
            }
        }
        private DelegateCommand<object> _SaveAsCommand;

        public ICommand SaveAsCommand
        {
            get
            {
                if (_SaveAsCommand != null) return _SaveAsCommand;
                _SaveAsCommand = new DelegateCommand<object>(onSaveCommand);
                return _SaveAsCommand;
            }
        }

        private DelegateCommand<object> _SaveCommand;

        public ICommand SaveCommand
        {
            get
            {
                if (_SaveCommand != null) return _SaveCommand;
                _SaveCommand = new DelegateCommand<object>(onSaveCommand);
                return _SaveCommand;
            }
        }
        private bool _IsUpdating;

        public bool IsUpdating
        {
            get { return _IsUpdating; }
            set
            {
                SetProperty(ref _IsUpdating, value);
            }
        }
        public override bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private string ProjectID { get; set; }
        private string CmpntOrLineID { get; set; }
        private string PdTitle { get; set; }
        private ObjectDefId ObjectDefId { get; set; }
        private readonly IDialogService _dialogService;
        private readonly IPDGeneralService pdServ;
        private readonly IInstrumentService TagServ;
        private readonly ILineService LineServ;
        private readonly IFlowService flowServ;
        private readonly ICodeListService codelistServ;
        private readonly IAnalyzerService analyzerServ;
        private readonly IPressureService pressureServ;
        private readonly ITemperatureService temperatureServ;
        public PdGeneralEditViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IUnityContainer unityContainer,
            IEventAggregator eventAggregator) : base(regionManager, unityContainer, dialog)
        {

            _regionManager = regionManager;
            _container = unityContainer;
            TagServ = unityContainer.Resolve<InstrumentService>();
            LineServ = unityContainer.Resolve<LineService>();

            pdServ = unityContainer.Resolve<PDGeneralService>();
            codelistServ = unityContainer.Resolve<CodeListService>();
            flowServ = unityContainer.Resolve<FlowService>();
            analyzerServ = unityContainer.Resolve<AnalyzerService>();
            pressureServ = unityContainer.Resolve<PressureService>();
            temperatureServ = unityContainer.Resolve<TemperatureService>();


            ProjectID = GlobalObject.CurrentProject.ProjId.ToString();
            //list_pdFluidPhase = GlobalObject.PropertyDropDownList.GetValueOrNull(GlobalObject.CurrentProject.ProjId.ToString() + "_" + 1312);
            list_pdFluidPhase = new ObservableCollection<DropDownListObj>();

            list_pdFlowUid = new ObservableCollection<DropDownListObj>();
            list_PdDensUid = new ObservableCollection<DropDownListObj>();
            list_PdPressUid = new ObservableCollection<DropDownListObj>();
            list_PdTempUid = new ObservableCollection<DropDownListObj>();
            list_PdViscUid = new ObservableCollection<DropDownListObj>();
            list_PdVelocityUid = new ObservableCollection<DropDownListObj>();
            list_PdVapPressUid = new ObservableCollection<DropDownListObj>();
            list_PdCriticPressUid = new ObservableCollection<DropDownListObj>();
            list_PdPressDesUid = new ObservableCollection<DropDownListObj>();
            list_PdPressDesMinUid = new ObservableCollection<DropDownListObj>();
            list_PdDesignTempUid = new ObservableCollection<DropDownListObj>();
            list_PdVapPressUflg = new ObservableCollection<DropDownListObj>();
            list_PdPressUflg = new ObservableCollection<DropDownListObj>();
            list_PdPressDesUflg = new ObservableCollection<DropDownListObj>();
            list_PdFlowUflg = new ObservableCollection<DropDownListObj>();
            list_PdCriticPressUflg = new ObservableCollection<DropDownListObj>();
            list_PdPressDesMinUflg = new ObservableCollection<DropDownListObj>();
            list_PdColored = new ObservableCollection<DropDownListObj>();
            list_LINE_ID = new ObservableCollection<DropDownListObj>();
            list_PdToxic = new ObservableCollection<DropDownListObj>();
            list_PdCorrosive = new ObservableCollection<DropDownListObj>();
            list_PdErosive = new ObservableCollection<DropDownListObj>();
            list_Case_Id = new ObservableCollection<DropDownListObj>();
            list_PdSource = new ObservableCollection<DropDownListObj>();
            list_PdFluidStat = new ObservableCollection<DropDownListObj>();
            list_PdPressDrpUid = new ObservableCollection<DropDownListObj>();
            list_PdPressDownUid = new ObservableCollection<DropDownListObj>();
            list_PdPressDownUflg = new ObservableCollection<DropDownListObj>();
            list_PdReturnTempUid = new ObservableCollection<DropDownListObj>();
            list_PdReturnPresUid = new ObservableCollection<DropDownListObj>();
            list_PdReturnPresUflg = new ObservableCollection<DropDownListObj>();


        }

        protected override async void OnInitPage(object obj)
        {
            var Ids = "1310,1294,1351,1394,1431,1426,1422,1283,1342,1340,1297,1421,1350,1341,1309,1282,1339,1346,13137,13138";
            await codelistServ.GetDropDownListsByAttrAsync(GlobalObject.CurrentProject.ProjId.ToString(), Ids.Split(',').ToList());

            list_pdFluidPhase.AddRange(await codelistServ.GetDropDownListByDefAsync("E2010", "enum"));


            list_pdFlowUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1310));
            list_PdDensUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1294));
            list_PdPressUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1351));
            list_PdTempUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1394));
            list_PdViscUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1431));
            list_PdVelocityUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1426));
            list_PdVapPressUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1422));
            list_PdCriticPressUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1283));
            list_PdPressDesUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1342));
            list_PdPressDesMinUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1340));
            list_PdDesignTempUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1297));
            list_PdVapPressUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1421));
            list_PdPressUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1350));
            list_PdPressDesUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1341));
            list_PdFlowUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1309));
            list_PdCriticPressUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1282));
            list_PdPressDesMinUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1339));

            list_LINE_ID.AddRange(await codelistServ.GetDropDownListByDefAsync(GlobalObject.lookupDic[nameof(Line)]));
            list_PdErosive.AddRange(await codelistServ.GetDropDownListByAttrAsync(1302));
            list_PdToxic.AddRange(await codelistServ.GetDropDownListByAttrAsync(1396));
            list_PdCorrosive.AddRange(await codelistServ.GetDropDownListByAttrAsync(1277));
            list_PdColored.AddRange(await codelistServ.GetDropDownListByAttrAsync(1266));
            list_Case_Id.AddRange(await codelistServ.GetDropDownListByAttrAsync(1233));
            list_PdFluidStat.AddRange(await codelistServ.GetDropDownListByAttrAsync(1313));
            list_PdSource.AddRange(await codelistServ.GetDropDownListByAttrAsync(1379));







        }
        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            IsUpdating = true;
            if (navigationContext.Parameters.ContainsKey(DialogParameterKeys.Id))
                CmpntOrLineID = navigationContext.Parameters[DialogParameterKeys.Id].ToString();
            if (navigationContext.Parameters.ContainsKey(DialogParameterKeys.ObjectType))
                ObjectDefId = (ObjectDefId)navigationContext.Parameters[DialogParameterKeys.ObjectType];
            if (navigationContext.Parameters.ContainsKey(DialogParameterKeys.Title))
                PdTitle = navigationContext.Parameters[DialogParameterKeys.Title].ToString();
            if (decimal.TryParse(CmpntOrLineID, out decimal cmpntId))
            {
                if (ObjectDefId == ObjectDefId.component)
                {
                    Title.TitleText = PdTitle;
                    data = await pdServ.GetProcessDataAsync(GlobalObject.CurrentProject.ProjId, ObjectDefId.component, cmpntId);

                    Componentdata = await TagServ.GetInstrumentAsync(GlobalObject.CurrentProject.ProjId, cmpntId.ToString());
                    Linedata = await LineServ.GetById(Componentdata.LINE_ID);
                    LineSize = Linedata.LineSize;
                    LineUom = Linedata.LineUom;
                    LineSche = Linedata.LineSched;

                }
                else if (ObjectDefId == ObjectDefId.line)
                {
                    Title.TitleText = PdTitle;
                    data = await pdServ.GetProcessDataAsync(GlobalObject.CurrentProject.ProjId, ObjectDefId.line, cmpntId);

                }
            }
            if (data != null)
            {
                IsControlGeneralVisible = true;
                IsControlFlowVisible = true;
                IsControlPressVisible = true;
                IsControlTempVisible = true;
                IsControlAnalyzerVisible = false;
                IsControlControlValveVisible = false;
                switch (Componentdata.PROC_FUNC_ID)
                {
                    case 0M:
                        IsControlGeneralVisible = false;
                        LabelText = "工作压力";
                        break;
                    case 1M:
                        IsControlFlowVisible = false;
                        LabelText = "工作压力";
                        Flowdata = await flowServ.GetById(Componentdata.CMPNT_ID);
                        AlarmLow = Flowdata.PdFAlarmLow;
                        AlarmLowLow = Flowdata.PdFAlarmLowLow;
                        AlarmLowLowLow = Flowdata.PdFAlarmLowLowLow;
                        AlarmHigh = Flowdata.PdFAlarmHigh;
                        AlarmHighHigh = Flowdata.PdFAlarmHighHigh;
                        AlarmHighHighHigh = Flowdata.PdFAlarmHighHighHigh;
                        TripLow = Flowdata.PdFTripLow;
                        TripLowLow = Flowdata.PdFTripLowLow;
                        TripLowLowLow = Flowdata.PdFTripLowLowLow;
                        TripHigh = Flowdata.PdFTripHigh;
                        TripHighHigh = Flowdata.PdFTripHighHigh;
                        TripHighHighHigh = Flowdata.PdFTripHighHighHigh;
                        break;
                    case 3M:
                        IsControlPressVisible = false;
                        LabelText = "工作压力";
                        Pressuredata = await pressureServ.GetById(Componentdata.CMPNT_ID);
                        AlarmLow = Pressuredata.PdPAlarmLow;
                        AlarmLowLow = Pressuredata.PdPAlarmLowLow;
                        AlarmLowLowLow = Pressuredata.PdPAlarmLowLowLow;
                        AlarmHigh = Pressuredata.PdPAlarmHigh;
                        AlarmHighHigh = Pressuredata.PdPAlarmHighHigh;
                        AlarmHighHighHigh = Pressuredata.PdPAlarmHighHighHigh;
                        TripLow = Pressuredata.PdPTripLow;
                        TripLowLow = Pressuredata.PdPTripLowLow;
                        TripLowLowLow = Pressuredata.PdPTripLowLowLow;
                        TripHigh = Pressuredata.PdPTripHigh;
                        TripHighHigh = Pressuredata.PdPTripHighHigh;
                        TripHighHighHigh = Pressuredata.PdPTripHighHighHigh;
                        break;
                    case 4M:
                        IsControlTempVisible = false;
                        LabelText = "工作压力";
                        Temperaturedata = await temperatureServ.GetById(Componentdata.CMPNT_ID);
                        AlarmLow = Temperaturedata.PdTAlarmLow;
                        AlarmLowLow = Temperaturedata.PdTAlarmLowLow;
                        AlarmLowLowLow = Temperaturedata.PdTAlarmLowLowLow;
                        AlarmHigh = Temperaturedata.PdTAlarmHigh;
                        AlarmHighHigh = Temperaturedata.PdTAlarmHighHigh;
                        AlarmHighHighHigh = Temperaturedata.PdTAlarmHighHighHigh;
                        TripLow = Temperaturedata.PdTTripLow;
                        TripLowLow = Temperaturedata.PdTTripLowLow;
                        TripLowLowLow = Temperaturedata.PdTTripLowLowLow;
                        TripHigh = Temperaturedata.PdTTripHigh;
                        TripHighHigh = Temperaturedata.PdTTripHighHigh;
                        TripHighHighHigh = Temperaturedata.PdTTripHighHighHigh;
                        break;
                    case 5M:
                        IsControlAnalyzerVisible = true;
                        LabelText = "工作压力";
                        Analyzerdata = await analyzerServ.GetById(Componentdata.CMPNT_ID);
                        AlarmLow = Analyzerdata.PdAAlarmLow;
                        AlarmLowLow = Analyzerdata.PdAAlarmLowLow;
                        AlarmLowLowLow = Analyzerdata.PdAAlarmLowLowLow;
                        AlarmHigh = Analyzerdata.PdAAlarmHigh;
                        AlarmHighHigh = Analyzerdata.PdAAlarmHighHigh;
                        AlarmHighHighHigh = Analyzerdata.PdAAlarmHighHighHigh;
                        TripLow = Analyzerdata.PdATripLow;
                        TripLowLow = Analyzerdata.PdATripLowLow;
                        TripLowLowLow = Analyzerdata.PdATripLowLowLow;
                        TripHigh = Analyzerdata.PdATripHigh;
                        TripHighHigh = Analyzerdata.PdATripHighHigh;
                        TripHighHighHigh = Analyzerdata.PdATripHighHighHigh;
                        break;
                    case 6M:
                        IsControlControlValveVisible = true;
                        LabelText = "上面压力";
                        break;
                    case 7M:
                        break;
                    // Add cases for other PROC_FUNC_ID values
                    default:
                        break;
                }
                IsCombinedControlVisible = IsControlGeneralVisible && IsControlPressVisible;
                switch (data.PdFluidPhase)
                {
                    case "L":
                        break;
                    default:
                        break;
                }
            }
            IsUpdating = false;
            RaisePropertyChanged(nameof(Title));
            RaisePropertyChanged(nameof(LabelText));
            RaisePropertyChanged(nameof(IsControlGeneralVisible));
            RaisePropertyChanged(nameof(IsControlControlValveVisible));
            RaisePropertyChanged(nameof(IsControlTempVisible));
            RaisePropertyChanged(nameof(IsControlFlowVisible));
            RaisePropertyChanged(nameof(IsControlAnalyzerVisible));
            RaisePropertyChanged(nameof(IsControlLevelVisible));
            RaisePropertyChanged(nameof(IsCombinedControlVisible));
            RaisePropertyChanged(nameof(IsControlPressVisible));
            RaisePropertyChanged(nameof(AlarmLow));
            RaisePropertyChanged(nameof(AlarmLowLow));
            RaisePropertyChanged(nameof(AlarmLowLowLow));
            RaisePropertyChanged(nameof(AlarmHigh));
            RaisePropertyChanged(nameof(AlarmHighHigh));

            RaisePropertyChanged(nameof(AlarmHighHighHigh));
            RaisePropertyChanged(nameof(TripLow));
            RaisePropertyChanged(nameof(TripLowLow));
            RaisePropertyChanged(nameof(TripLowLowLow));
            RaisePropertyChanged(nameof(TripHigh));
            RaisePropertyChanged(nameof(TripHighHigh));
            RaisePropertyChanged(nameof(TripHighHighHigh));
        }


        #region command
        private async void onSaveCommand(object obj)
        {
            var x = data.PdFluidName;
            var res = await pdServ.UpdateProcessDataAsync(Convert.ToDecimal(ProjectID), data);
            var res2 = await TagServ.UpdateInstrumentAsync(Convert.ToDecimal(ProjectID), Componentdata);
            switch (Componentdata.PROC_FUNC_ID)
            {
                case 0M:
                    break;
                case 1M:
                    Flowdata.PdFAlarmLow = AlarmLow;
                    Flowdata.PdFAlarmLowLow = AlarmLowLow;
                    Flowdata.PdFAlarmLowLowLow = AlarmLowLowLow;
                    Flowdata.PdFAlarmHigh = AlarmHigh;
                    Flowdata.PdFAlarmHighHigh = AlarmHighHigh;
                    Flowdata.PdFAlarmHighHighHigh = AlarmHighHighHigh;
                    Flowdata.PdFTripLow = TripLow;
                    Flowdata.PdFTripLowLow = TripLowLow;
                    Flowdata.PdFTripLowLowLow = TripLowLowLow;
                    Flowdata.PdFTripHigh = TripHigh;
                    Flowdata.PdFTripHighHigh = TripHighHigh;
                    Flowdata.PdFTripHighHighHigh = TripHighHighHigh;

                    var res3 = await flowServ.UpdateFlowAsync(Convert.ToDecimal(ProjectID), Flowdata);
                    if (res && res2 && res3)
                    {
                        Dialog.ShowMessage("保存成功！");
                    }
                    {

                    }
                    break;
                case 3M:
                    Pressuredata.PdPAlarmLow = AlarmLow;
                    Pressuredata.PdPAlarmLowLow = AlarmLowLow;
                    Pressuredata.PdPAlarmLowLowLow = AlarmLowLowLow;
                    Pressuredata.PdPAlarmHigh = AlarmHigh;
                    Pressuredata.PdPAlarmHighHigh = AlarmHighHigh;
                    Pressuredata.PdPAlarmHighHighHigh = AlarmHighHighHigh;
                    Pressuredata.PdPTripLow = TripLow;
                    Pressuredata.PdPTripLowLow = TripLowLow;
                    Pressuredata.PdPTripLowLowLow = TripLowLowLow;
                    Pressuredata.PdPTripHigh = TripHigh;
                    Pressuredata.PdPTripHighHigh = TripHighHigh;
                    Pressuredata.PdPTripHighHighHigh = TripHighHighHigh;

                    var res4 = await pressureServ.UpdatePressureAsync(Convert.ToDecimal(ProjectID), Pressuredata);
                    if (res && res2 && res4)
                    {
                        Dialog.ShowMessage("保存成功！");
                    }
                    {

                    }
                    break;
                case 4M:
                    Temperaturedata.PdTAlarmLow = AlarmLow;
                    Temperaturedata.PdTAlarmLowLow = AlarmLowLow;
                    Temperaturedata.PdTAlarmLowLowLow = AlarmLowLowLow;
                    Temperaturedata.PdTAlarmHigh = AlarmHigh;
                    Temperaturedata.PdTAlarmHighHigh = AlarmHighHigh;
                    Temperaturedata.PdTAlarmHighHighHigh = AlarmHighHighHigh;
                    Temperaturedata.PdTTripLow = TripLow;
                    Temperaturedata.PdTTripLowLow = TripLowLow;
                    Temperaturedata.PdTTripLowLowLow = TripLowLowLow;
                    Temperaturedata.PdTTripHigh = TripHigh;
                    Temperaturedata.PdTTripHighHigh = TripHighHigh;
                    Temperaturedata.PdTTripHighHighHigh = TripHighHighHigh;

                    var res5 = await temperatureServ.UpdateTemperatureAsync(Convert.ToDecimal(ProjectID), Temperaturedata);
                    if (res && res2 && res5)
                    {
                        Dialog.ShowMessage("保存成功！");
                    }
                    {

                    }
                    break;
                case 5M:
                    Analyzerdata.PdAAlarmLow = AlarmLow;
                    Analyzerdata.PdAAlarmLowLow = AlarmLowLow;
                    Analyzerdata.PdAAlarmLowLowLow = AlarmLowLowLow;
                    Analyzerdata.PdAAlarmHigh = AlarmHigh;
                    Analyzerdata.PdAAlarmHighHigh = AlarmHighHigh;
                    Analyzerdata.PdAAlarmHighHighHigh = AlarmHighHighHigh;
                    Analyzerdata.PdATripLow = TripLow;
                    Analyzerdata.PdATripLowLow = TripLowLow;
                    Analyzerdata.PdATripLowLowLow = TripLowLowLow;
                    Analyzerdata.PdATripHigh = TripHigh;
                    Analyzerdata.PdATripHighHigh = TripHighHigh;
                    Analyzerdata.PdATripHighHighHigh = TripHighHighHigh;

                    var res6 = await analyzerServ.UpdateAnalyzerAsync(Convert.ToDecimal(ProjectID), Analyzerdata);
                    if (res && res2 && res6)
                    {
                        Dialog.ShowMessage("保存成功！");
                    }
                    {
                    }

                    break;
                case 6M:

                    break;
                case 7M:
                    break;
                // Add cases for other PROC_FUNC_ID values
                default:
                    break;



            }
            #endregion
        }

        private  void onRevisionCommand(object obj)
        {
            Prism.Services.Dialogs.DialogParameters dialogParameters = new Prism.Services.Dialogs.DialogParameters();
            dialogParameters.Add(DialogParameterKeys.Id, data.DwgId);
            Dialog.ShowDialog(nameof(RevisionManager), dialogParameters,null);
        }
    }
}
